package com.android.common.utils.media.screen;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;

;

public class ScreenHelper {

    private static class InstanceHolder {
        private static final ScreenHelper instance = new ScreenHelper();
    }

    public static ScreenHelper getInstance() {
        return InstanceHolder.instance;
    }

    private ScreenHelper() {
        super();
    }

    private ServiceConnection mRecordScreenServiceConnection;

    private ServiceConnection mCaptureScreenServiceConnection;

    private RecordScreenService mRecordScreenService;

    private CaptureScreenService mCaptureScreenService;


    /**
     * 启动媒体投影服务
     *
     * @param activity activity
     */
    public void startRecordScreenService(Activity activity, RecordScreenCallBack captureCallBack) {
        // 绑定服务
        mRecordScreenServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                if (service instanceof RecordScreenService.MediaProjectionBinder) {
                    mRecordScreenService = ((RecordScreenService.MediaProjectionBinder) service).getService();
                    mRecordScreenService.permissionMediaProjection((resultCode, data) -> {
                        mRecordScreenService.startScreenRecording(resultCode,data,captureCallBack);
                    });
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                mRecordScreenService = null;
            }
        };
        RecordScreenService.bindService(activity, mRecordScreenServiceConnection);
    }

    public void stopRecordScreenService(RecordScreenCallBack captureCallBack) {
        if (mRecordScreenServiceConnection != null) {
            mRecordScreenService.stopScreenRecording(captureCallBack);
            mRecordScreenService.unbindService(mRecordScreenServiceConnection);
            mRecordScreenServiceConnection = null;
        }
    }

    /**
     * 启动媒体投影服务
     *
     * @param activity activity
     */
    public void captureScreenService(Activity activity, RecordScreenCallBack captureCallBack) {
        // 绑定服务
        mCaptureScreenServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                if (service instanceof CaptureScreenService.MediaProjectionBinder) {
                    mCaptureScreenService = ((CaptureScreenService.MediaProjectionBinder) service).getService();
                    mCaptureScreenService.permissionMediaProjection((resultCode, data) -> {
                        mCaptureScreenService.captureScreen(resultCode,data,captureCallBack);
                    });
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                mCaptureScreenService = null;
            }
        };
        CaptureScreenService.bindService(activity, mCaptureScreenServiceConnection);
    }

    public void stopCaptureScreenService(RecordScreenCallBack captureCallBack) {
        if (mCaptureScreenServiceConnection != null) {
            mCaptureScreenService.unbindService(mCaptureScreenServiceConnection);
            mCaptureScreenServiceConnection = null;
        }
    }
}
